package yxy.game.pm2.command.union.tech;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.pubsub.CurrencyPublisher;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.player.UnionPlayer;
import yxy.game.pm2.module.union.player.tech.UnionPlayerTech;
import yxy.game.pm2.module.union.player.tech.UnionTechHandle;

import java.util.Collection;
import java.util.Date;

/** 联盟科技 联盟科技点亮 */
@MessageType(proto = Command.UnionTechLight, classtype = UnionMessage.UnionTechLightRequest.class)
public class CMD_UnionTechLight extends Command<UnionMessage.UnionTechLightRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionMessage.UnionTechLightRequest request) {
		Date date = new Date();
		String playerCode = messageSource.getTag();
		int dataId = request.getDataId();

		UnionMessage.UnionTechLightResponse.Builder response = UnionMessage.UnionTechLightResponse.newBuilder();

		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnion(entry);
		if (!result.equals(Result.UnionIn)) {
			send(messageSource, response, result);
			return;
		}
		RedisLock uLock = UnionBean.lock(entry.getUnionBean().getCode());
		RedisLock pLock = UnionPlayer.lock(entry.getPlayerCode());
		try {
			if (uLock.tryLock() && pLock.tryLock()) {
				UnionBean union = entry.reloadUnionBean();
				UnionPlayer unionPlayer = entry.reloadUnionPlayer();
				UnionPlayerTech playerTech = unionPlayer.getTech();
				if (playerTech.max(dataId)) {
					send(messageSource, response, Result.UnionTechlevelUpper);// 科技满级
					return;
				}

				Collection<Currency> costs = playerTech.cost(dataId);
				try {
					new CurrencyHandle().cost(playerCode, CCReason.联盟科技, costs);
				} catch (CurrencyNotEnoughException e) {
					send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
					return;
				}

				response.setTech(new UnionTechHandle().packTech(playerTech.upgrade(dataId)));
				unionPlayer.set();

				send(messageSource, response, Result.Success);

				PlayTypeHandle.saveUnion(playerCode, PlayTypeModule.get(playerCode));
				new BattleHandle().powerUpdate(playerCode);
				new UnionHandle().powerUpdate(union);

				CurrencyPublisher.getInstance().publishMinus(playerCode, costs);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			uLock.unlock();
			pLock.unlock();
		}
		Date date1 = new Date();
		System.out.println("CMD_UnionTechLight:"+(date1.getTime()-date.getTime()));
	}

	private void send(MessageSource messageSource, UnionMessage.UnionTechLightResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}